/*
* @Author: dengjiayao
* @Date:   2017-12-19 09:32:38
 * @Last Modified by: songwh
 * @Last Modified time: 2018-01-09 09:23:00
*/

import Vue from 'vue'

const SUCCESS = 1000000
const API_ROOT = server.root + '/newmanage'

const getClassesListResource = Vue.resource(API_ROOT + '/cms/course/list')
export const getClassesList = ({ dispatch }, params) => {
  return getClassesListResource.save(params).then(res => {
    dispatch('CLASSES_LIST_NEED_FRESH', 0)
    if (+res.data.code === SUCCESS) {
      return Promise.resolve(res.data.data)
    }
    return Promise.reject(res.data)
  })
}

const saveClassesResource = Vue.resource(API_ROOT + '/cms/course/save')
export const saveClasses = ({ dispatch }, params) => {
  return saveClassesResource.save(params).then(res => {
    dispatch('CLASSES_LIST_NEED_FRESH', 1)
    if (+res.data.code === SUCCESS) {
      return Promise.resolve(res.data.data)
    }
    return Promise.reject(res.data)
  })
}

const deleteClassesResource = Vue.resource(API_ROOT + '/cms/course/delete')
export const deleteClasses = ({ dispatch }, params) => {
  return deleteClassesResource.save(params).then(res => {
    dispatch('CLASSES_LIST_NEED_FRESH', 1)
    if (+res.data.code === SUCCESS) {
      return Promise.resolve(res.data.data)
    }
    return Promise.reject(res.data)
  })
}

const getSubscribeCourseListResource = Vue.resource(API_ROOT + '/subscribe/course/list')
export const getSubscribeCourseList = ({ dispatch }, params) => {
  return getSubscribeCourseListResource.save(params).then(res => {
    if (+res.data.code === SUCCESS) {
      return Promise.resolve(res.data.data)
    }
    return Promise.reject(res.data)
  })
}

const getSubscribeCourseDurationResource = Vue.resource(API_ROOT + '/subscribe/course/duration')
export const getSubscribeCourseDuration = ({ dispatch }, params) => {
  return getSubscribeCourseDurationResource.save(params).then(res => {
    if (+res.data.code === SUCCESS) {
      return Promise.resolve(res.data.data.durationList)
    }
    return Promise.reject(res.data)
  })
}

const subscribeCourseResource = Vue.resource(API_ROOT + '/subscribe/course/subscribe')
export const subscribeCourse = ({ dispatch }, params) => {
  return subscribeCourseResource.save(params).then(res => {
    dispatch('CLASSES_LIST_NEED_FRESH', 1)
    if (+res.data.code === SUCCESS) {
      return Promise.resolve(res.data.data)
    }
    return Promise.reject(res.data)
  })
}
/**
 * 大转盘抽奖活动发课程
 */

const prizeSubscribeCourseResource = Vue.resource(API_ROOT + '/prize/course/subscribe')
export const prizeSubscribeCourse = ({ dispatch }, params) => {
  return prizeSubscribeCourseResource.save(params).then(res => {
    dispatch('CLASSES_LIST_NEED_FRESH', 1)
    if (+res.data.code === SUCCESS) {
      return Promise.resolve(res.data.data)
    }
    return Promise.reject(res.data)
  })
}

const unsubscribeCourseResource = Vue.resource(API_ROOT + '/subscribe/unSubscribeItem')
export const unsubscribeCourse = ({ dispatch }, params) => {
  return unsubscribeCourseResource.save(params).then(res => {
    dispatch('CLASSES_LIST_NEED_FRESH', 1)
    if (+res.data.code === SUCCESS) {
      return Promise.resolve(res.data.data)
    }
    return Promise.reject(res.data)
  })
}

const getSubscribeUserListResource = Vue.resource(API_ROOT + '/subscribe/mysubscribelist')
export const getSubscribeUserList = ({ dispatch }, params) => {
  return getSubscribeUserListResource.save(params).then(res => {
    dispatch('CLASSES_LIST_NEED_FRESH', 0)
    if (+res.data.code === SUCCESS) {
      return Promise.resolve(res.data.data)
    }
    return Promise.reject(res.data)
  })
}
